import { parse } from 'url';
// mock tableListDataSource
let tableListDataSource = [];

for (let i = 0; i < 10; i += 1) {
  tableListDataSource.push({
  key: i,
  orderStatus:'范文',//	状态0草稿、1审核中、2通过、3生效、4关闭,5未通过
  type:'范文'	,//类型 0改签,1退票
  orderId:'范文'	,//赔偿单号
  overBookId:'范文'	,//超售单号
  flightNo:'范文'	,//航班号
  flightDate:'范文',//	航班日期
  std:'范文'	,//计划起飞时间
  serviceCity:	'范文',//	服务航站
  paxName:'范文',//	旅客姓名
  paxId:'范文',//	旅客ID
  tktNo:'范文',//票号
  segment:'范文'	,//航段
  totalMoney:'范文',//	总金额
  payMoney:'范文',//	赔偿金额
  price:'范文'	,//客票差价
  receiveStatus:'范文',//	领取状态(0未领取,1已领取,2处理中)
  endorseFlightNo:'范文',//	改签航班
  endorseFlightDate	:'范文',//	改签航班时间
  endorseStd:'范文'	,//改签时间计划起飞时间
  createUser:'范文'	,//申请人
  auditor:'范文',//	审核人
  grantUser:'范文',//	发放人
  });
}

function getRule(req, res, u) {
  let url = u;

  if (!url || Object.prototype.toString.call(url) !== '[object String]') {
    // eslint-disable-next-line prefer-destructuring
    url = req.url;
  }

  const params = parse(url, true).query;
  let dataSource = tableListDataSource;

  if (params.sorter) {
    const s = params.sorter.split('_');
    dataSource = dataSource.sort((prev, next) => {
      if (s[1] === 'descend') {
        return next[s[0]] - prev[s[0]];
      }

      return prev[s[0]] - next[s[0]];
    });
  }

  if (params.status) {
    const status = params.status.split(',');
    let filterDataSource = [];
    status.forEach(s => {
      filterDataSource = filterDataSource.concat(
        dataSource.filter(item => {
          if (parseInt(`${item.status}`, 10) === parseInt(s.split('')[0], 10)) {
            return true;
          }

          return false;
        }),
      );
    });
    dataSource = filterDataSource;
  }

  if (params.name) {
    dataSource = dataSource.filter(data => data.name.includes(params.name || ''));
  }

  let pageSize = 10;

  if (params.pageSize) {
    pageSize = parseInt(`${params.pageSize}`, 0);
  }

  const result = {
    data: dataSource,
    total: dataSource.length,
    success: true,
    pageSize,
    current: parseInt(`${params.currentPage}`, 10) || 1,
  };
  return res.json(result);
}

function postRule(req, res, u, b) {
  let url = u;

  if (!url || Object.prototype.toString.call(url) !== '[object String]') {
    // eslint-disable-next-line prefer-destructuring
    url = req.url;
  }

  const body = (b && b.body) || req.body;
  const { method, name, desc, key } = body;

  switch (method) {
    /* eslint no-case-declarations:0 */
    case 'delete':
      tableListDataSource = tableListDataSource.filter(item => key.indexOf(item.key) === -1);
      break;

    case 'post':
      const i = Math.ceil(Math.random() * 10000);
      tableListDataSource.unshift({
        key: i,
        orderStatus:'范文',//	状态0草稿、1审核中、2通过、3生效、4关闭,5未通过
        type:'范文'	,//类型 0改签,1退票
        orderId:'范文'	,//赔偿单号
        overBookId:'范文'	,//超售单号
        flightNo:'范文'	,//航班号
        flightDate:'范文',//	航班日期
        std:'范文'	,//计划起飞时间
        serviceCity:	'范文',//	服务航站
        paxName:'范文',//	旅客姓名
        paxId:'范文',//	旅客ID
        tktNo:'范文',//票号
        segment:'范文'	,//航段
        totalMoney:'范文',//	总金额
        payMoney:'范文',//	赔偿金额
        price:'范文'	,//客票差价
        receiveStatus:'范文',//	领取状态(0未领取,1已领取,2处理中)
        endorseFlightNo:'范文',//	改签航班
        endorseFlightDate	:'范文',//	改签航班时间
        endorseStd:'范文'	,//改签时间计划起飞时间
        createUser:'范文'	,//申请人
        auditor:'范文',//	审核人
        grantUser:'范文',//	发放人
      });
      break;

    case 'update':
      tableListDataSource = tableListDataSource.map(item => {
        if (item.key === key) {
          return { ...item, desc, name };
        }

        return item;
      });
      break;

    default:
      break;
  }

  const result = {
    list: tableListDataSource,
    pagination: {
      total: tableListDataSource.length,
    },
  };
  return res.json(result);
}

export default {
  'GET /api/overSale/index': getRule,
  'POST /api/overSale/index': postRule,
};
